package com.Lixiaoqian.CardPlay.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.Lixiaoqian.CardPlay.R;
import com.Lixiaoqian.CardPlay.base.BaseActivity;
import com.Lixiaoqian.CardPlay.utils.AnimotionUtils;
import com.Lixiaoqian.CardPlay.utils.Constant;
import com.Lixiaoqian.CardPlay.utils.DialogUtils;

import butterknife.BindView;

public class CommonWebActivity extends BaseActivity {

    @BindView(R.id.webView)
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWEbView();
    }

    private void initWEbView() {
        DialogUtils.showCustomDialogFragment(CommonWebActivity.this);
        webView.loadUrl(getIntent().getStringExtra(Constant.INTENT_WEB_URL));
        // webView.canGoBack();
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    DialogUtils.dismissProgress();
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        webView.setOnTouchListener(new View.OnTouchListener() {

            private float y;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        y = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        float moveY = event.getY();
                        float v1 = moveY - y;
                        Log.i("movey", "onTouch: "+v1);
                        if (moveY - y >100) {
                            if (!actionBar.isShowing()) {
                                actionBar.show();
                                AnimotionUtils.AnimotionShow(CommonWebActivity.this, R.anim.anim_alph_show, toolbar);
                            }
                        }
                        if (moveY - y < -100) {
                            if (actionBar.isShowing()) {
                                actionBar.hide();
                                AnimotionUtils.AnimotionShow(CommonWebActivity.this, R.anim.anim_alph_hide, toolbar);
                            }
                        }
                        break;

                }
                return false;
            }
        });
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_common_web;
    }

    @Override
    protected void initToolbar() {
        super.initToolbar();
        actionBar.setTitle(getIntent().getStringExtra(Constant.INTENT_ACTIVITY_TITLE));
    }

    public static void launch(Activity fromActivity, String title, String url) {
        Intent intent = new Intent(fromActivity, CommonWebActivity.class);
        intent.putExtra(Constant.INTENT_ACTIVITY_TITLE, title);
        intent.putExtra(Constant.INTENT_WEB_URL, url);
        fromActivity.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView = null;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (webView.canGoBack()){
            webView.goBack();
            return true;
        }else
        return super.onOptionsItemSelected(item);
    }


}
